package com.weiadmin.controller.system;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weiadmin.common.BaseController;
import com.weiadmin.common.PageParam;
import com.weiadmin.dao.RoleDao;
import com.weiadmin.dao.UserDao;
import com.weiadmin.model.system.RoleModel;
import com.weiadmin.model.system.SlideModel;
import com.weiadmin.model.system.UserModel;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 *
 *用户管理模块
 * @author: shizhongyu
 * @date: 2023年06月03日 18:34
 */
@RestController
@RequestMapping("/admin/user")
public class UserController extends BaseController {
    private String module = "user";
    @Autowired
    UserDao dao;

    @Autowired
    RoleDao roleDao;
    @GetMapping("/index")
    public ModelAndView index(Model model) {
        ModelAndView modelAndView = display(this.module+"/index");
        return modelAndView;
    }
    @GetMapping("/list")
    public ResponseEntity<?> list(PageParam pageParam)
    {
        Page<UserModel> result =  dao.getList(pageParam);
        return page("请求成功",result);
    }
    @GetMapping("/add")
    public ModelAndView add(Model model) {
        ModelAndView modelAndView = display(this.module+"/add");
        QueryWrapper<RoleModel> queryWrapper = new QueryWrapper<>();
        List<RoleModel> list = roleDao.selectList(queryWrapper);
        model.addAttribute("roleList",list);
        return modelAndView;
    }
    @GetMapping("/edit")
    public ModelAndView edit(Model model,String id) {
        ModelAndView modelAndView = display(this.module+"/edit");
        UserModel item =  dao.selectById(id);
        model.addAttribute("item",item);
        //新增角色列表
        QueryWrapper<RoleModel> queryWrapper = new QueryWrapper<>();
        List<RoleModel> list = roleDao.selectList(queryWrapper);
        model.addAttribute("roleList",list);
        return modelAndView;
    }



    @PostMapping("/addPost")
    public ResponseEntity<?> addPost(@RequestBody UserModel params)
    {
        if(params.getUserPass()!=null){
            String password  = DigestUtil.md5Hex(params.getUserPass());;
            params.setUserPass(password);
        }
        int result = dao.insert(params);
        if(result>0){
            return success("添加成功！");
        }else{
            return error("添加失败,请稍后再试！");
        }
    }

    @PostMapping("/editPost")
    public ResponseEntity<?> editPost(@RequestBody UserModel params)
    {
        if(params.getUserPass()!=null){
            String password  = DigestUtil.md5Hex(params.getUserPass());;
            params.setUserPass(password);
        }
        int result = dao.updateById(params);
        if(result>0){
            return success("保存成功！");
        }else{
            return error("保存失败,请稍后再试！");
        }
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<?> delete(@PathVariable String id)
    {
        if(StringUtils.isEmpty(id)){
            return error("ID不能为空！");
        }
        int result = dao.deleteById(id);
        if(result>0){
            return success("删除成功！");
        }else{
            return error("删除失败,请稍后再试！");
        }
    }
}
